package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.helper.LoginHelper;
import com.joker.common.utils.StringUtils;
import com.joker.orange.domain.Comment;
import com.joker.orange.domain.PlayRecord;
import com.joker.orange.domain.bo.PlayRecordBo;
import com.joker.orange.domain.vo.PlayRecordVo;
import com.joker.orange.mapper.PlayRecordMapper;
import com.joker.orange.service.IPlayRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class PlayRecordServiceImpl implements IPlayRecordService {

    private final PlayRecordMapper baseMapper;

    /**
     * 查询播放记录
     */
    @Override
    public PlayRecordVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public PlayRecord queryPlayRecord(PlayRecordBo bo) {
        return baseMapper.selectOne(buildQueryWrapper(bo));
    }

    /**
     * 查询播放记录列表
     */
    @Override
    public TableDataInfo<PlayRecordVo> queryPageList(PlayRecordBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        QueryWrapper<PlayRecord> lqw = Wrappers.query();
        lqw.eq(bo.getUserId() != null, "p.user_id", bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoType()), "p.video_type", bo.getVideoType());
        lqw.orderByDesc("p.create_time");
        Page<PlayRecordVo> result = baseMapper.selectPlayRecordVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询播放记录列表
     */
    @Override
    public List<PlayRecordVo> queryList(PlayRecordBo bo) {
        LambdaQueryWrapper<PlayRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PlayRecord> buildQueryWrapper(PlayRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PlayRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, PlayRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getVideoId() != null, PlayRecord::getVideoId, bo.getVideoId());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoType()), PlayRecord::getVideoType, bo.getVideoType());
        lqw.orderByDesc(PlayRecord::getCreateTime);
        return lqw;
    }

    /**
     * 新增播放记录
     */
    @Override
    public Boolean insertByBo(PlayRecordBo bo) {
        PlayRecord add = BeanUtil.toBean(bo, PlayRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改播放记录
     */
    @Override
    public Boolean updateByBo(PlayRecordBo bo) {
        PlayRecord update = BeanUtil.toBean(bo, PlayRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PlayRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除播放记录
     */
    @Override
    public Boolean deleteWithValidByIds(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
